package com.example.administrator.qimo3no74.home;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.qimo3no74.GoodsActivity;
import com.example.administrator.qimo3no74.R;

import java.util.List;

public class CommodityAdapter extends RecyclerView.Adapter<CommodityAdapter.ViewHolder> {

    private List<Commodity> commodities;

    static  class ViewHolder extends RecyclerView.ViewHolder{
        ImageView commodityImg;
        TextView commodityName;
        TextView commodityPrice;
        TextView commodityPeople;


        public ViewHolder(View itemView, final Context context){
            super(itemView);
            this.commodityImg = itemView.findViewById(R.id.commodityImageView);
            this.commodityName = itemView.findViewById(R.id.commodityTextViewName);
            this.commodityPrice = itemView.findViewById(R.id.commodityTextViewPrice);
            this.commodityPeople = itemView.findViewById(R.id.commodityTextViewPeople);

            commodityImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(view.getContext(),GoodsActivity.class);
                    view.getContext().startActivity(intent);
                }
            });
            commodityName.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context,""+commodityName.getText().toString(),Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    public CommodityAdapter(List<Commodity> commodities){
        this.commodities = commodities;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.commodity_item,viewGroup,false);
        ViewHolder holder = new ViewHolder(view,viewGroup.getContext());
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Commodity commodity = commodities.get(i);
        viewHolder.commodityImg.setImageResource(commodity.getCommodityImgId());
        viewHolder.commodityName.setText(commodity.getCommodityName());
        viewHolder.commodityPrice.setText(commodity.getCommodityPrice().toString());
        viewHolder.commodityPeople.setText(commodity.getCommodityPeople());
    }

    @Override
    public int getItemCount() {
        return commodities.size();
    }

}
